<?php

/**
 * 73YC 首页文件
 * ============================================================================
 * * 版权所有 20013-* 梅州络客网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.alork.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2013-10-19 06:29:08Z liubo $
*/
if (!defined('IN_ECS'))
{
    define('IN_ECS', true);
}

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );  
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache");

/* 显示程序运行时间 */
// define('MICROTIME',microsecond());
// register_shutdown_function('print_time');
// function print_time(){
//     echo microsecond()-MICROTIME;
// }
// function microsecond(){
//     list($usec, $sec) = explode(" ",microtime());
//     return ((float)$usec + (float)$sec);
// }

/* if ($_SERVER['REMOTE_ADDR'] != '60.186.86.63' && $_SERVER['HTTP_X_REAL_IP'] != '60.186.86.63' && $_SERVER['REMOTE_ADDR'] != '115.196.252.202' && $_SERVER['HTTP_X_REAL_IP'] != '115.196.252.202' && $_SERVER['REMOTE_ADDR'] != '183.129.201.250' && $_SERVER['HTTP_X_REAL_IP'] != '183.129.201.250') {
    echo $_SERVER['HTTP_X_REAL_IP'];
    die('非法IP入侵');
} */

// /*是否为手机端*/
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
$uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|mobile)/i";
if(!defined('IS_MOBILE') && (preg_match($uachar, $ua) || $_SERVER['HTTP_HOST'] == 'm.eggms.com') && !strpos(strtolower($_SERVER['REQUEST_URI']),'wap'))
{
    // define('IS_MOBILE', true);
    // header('HTTP/1.1 301 Moved Permanently');
    // header('location:http://m.eggms.com/mobile'.$_SERVER['REQUEST_URI']);
    // exit;
}

// if($_SERVER['HTTP_HOST'] == 'm.eggms.com'){
//     header('HTTP/1.1 301 Moved Permanently');
//     header('location:http://m.eggms.com/mobile'.$_SERVER['REQUEST_URI']);
//     exit;
// }


error_reporting(E_ALL);

if (__FILE__ == '')
{
    die('Fatal error code: 0');
}

/* 取得当前ecshop所在的根目录 */
define('ROOT_PATH', '/lanmp/www/eggmstest/public_html/');
define('ROOT', '/lanmp/www/eggmstest/public_html/');

/* 初始化设置 */
@ini_set('memory_limit',          '64M');
@ini_set('session.cache_expire',  3600);
@ini_set('session.use_trans_sid', 0);
@ini_set('session.use_cookies',   1);
@ini_set('session.auto_start',    0);
@ini_set('display_errors',        0);

if (DIRECTORY_SEPARATOR == '\\')
{
    @ini_set('include_path', '.;' . ROOT_PATH);
}
else
{
    @ini_set('include_path', '.:' . ROOT_PATH);
}

require(ROOT_PATH . 'data/config.php');

if (PHP_VERSION >= '5.1' && !empty($timezone))
{
    date_default_timezone_set($timezone);
}

$php_self = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
if ('/' == substr($php_self, -1))
{
    $php_self .= 'index.php';
}
define('PHP_SELF', $php_self);
/* 加载共用函数库和类库 */
require(ROOT_PATH . 'includes/funBase.php'); // 新版基础函数库
require(ROOT_PATH . 'includes/funCommon.php'); // 新版公共函数库
require(ROOT_PATH . 'includes/inc_constant.php'); // 常量
require(ROOT_PATH . 'includes/cls_ecshop.php'); // 原ecshop类库（待删除）
require(ROOT_PATH . 'includes/cls_error.php'); // 错误类库（待删除）
require(ROOT_PATH . 'includes/lib_time.php'); // 处理时间函数库（待删除）
require(ROOT_PATH . 'includes/lib_base.php'); // 原基础函数库 （待删除）
require(ROOT_PATH . 'includes/lib_common.php'); // 原公共函数库（待删除）
require(ROOT_PATH . 'includes/lib_main.php'); // 原主要函数库（待删除）
require(ROOT_PATH . 'includes/lib_insert.php'); // 原动态模板加载函数库（待删除）
require(ROOT_PATH . 'includes/lib_goods.php'); // 原商品函数库（待删除）
require(ROOT_PATH . 'includes/lib_article.php'); // 原文章函数库（待删除）

/* 对用户传入的变量进行转义操作。*/
if (!get_magic_quotes_gpc())
{
    if (!empty($_GET))
    {
        $_GET  = addslashes_deep($_GET);
    }
    if (!empty($_POST))
    {
        $_POST = addslashes_deep($_POST);
    }
    $_COOKIE   = addslashes_deep($_COOKIE);
    $_REQUEST  = addslashes_deep($_REQUEST);
}

/* 创建新对象 */
$ecs = new ECS($_CFG['dbName'], $_CFG['prefix']);
define('DATA_DIR', $ecs->data_dir()); // data目录（待删除）
define('IMAGE_DIR', $ecs->image_dir()); // 图片路径（待删除）

/* 初始化新版数据库类 */
require(ROOT_PATH . 'includes/classMysqli.php');
$db = new classMysqli($_CFG['dbHost'], $_CFG['dbUser'], $_CFG['dbPass'], $_CFG['dbName'],$_CFG['charset'],$_CFG['prefix'], 0, $_CFG['dbRead']);

$db->setMaxCacheTime($_CFG['dbCacheTime']);

// 实例化mem类
$mem = new memcached();
// 以二进制进行传输，当要进行密码验证时，这个一定要为true
$mem->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
// 添加服务池
$mem->addServer("{$_CFG['memcachedServer'][0]['host']}", "{$_CFG['memcachedServer'][0]['port']}");
// 进行密码验证
$mem->setSaslAuthData("{$_CFG['memcachedServer'][0]['user']}","{$_CFG['memcachedServer'][0]['pass']}");
// 取消自动压缩，如果要后面追加数据，不可压缩数据
$mem->setOption(Memcached::OPT_COMPRESSION, false);

/* 创建错误处理对象 */
$err = new ecs_error('message.dwt');

/* 载入系统参数 */
$_CFG = array_merge($_CFG,load_config());

/* 载入语言文件 */
require(ROOT_PATH . 'languages/' . $_CFG['lang'] . '/common.php');

if ($_CFG['shop_closed'] == 1)
{
    /* 商店关闭了，输出关闭的消息 */
    header('Content-type: text/html; charset='.EC_CHARSET);

    die('<div style="margin: 150px; text-align: center; font-size: 14px"><p>' . $_LANG['shop_closed'] . '</p><p>' . $_CFG['close_comment'] . '</p></div>');
}

if (is_spider())
{
    /* 如果是蜘蛛的访问，那么默认为访客方式，并且不记录到日志中 */
    if (!defined('INIT_NO_USERS'))
    {
        define('INIT_NO_USERS', true);
    }
    $_SESSION = array();
    $_SESSION['user_id']     = 0;
    $_SESSION['user_name']   = '';
    $_SESSION['email']       = '';
    $_SESSION['user_rank']   = 0;
    $_SESSION['discount']    = 1.00;
}

if (!defined('INIT_NO_USERS'))
{
    ini_set("session.cookie_domain", "test.eggms.com");
    ini_set("session.gc_maxlifetime", "86400");
    session_start();
    define('SESS_ID', session_id());

}

if(isset($_SERVER['PHP_SELF']))
{
    $_SERVER['PHP_SELF'] = htmlspecialchars($_SERVER['PHP_SELF']);
}

if (!defined('INIT_NO_SMARTY'))
{
    header('Cache-control: private');
    header('Content-type: text/html; charset=utf-8');
    /* 新项目路径 */
    $isNewItem = array('/index.php', '/mobile/index.php', '/tvd.php', '/mobile/tvd.php', '/tvd_list.php', '/mobile/tvd_list.php', '/shop.php', '/buyone.php', '/mobile/buyone.php', '/genuine.php', '/glairPoject.php', '/mobile/glairPoject.php', '/glair.php', '/mobile/glair.php', '/app_introduction.php','/freeplan.php','/mobile/freeplan.php');

    if (!in_array($php_self, $isNewItem)) {
        /* 创建 Smarty 对象。*/
        require(ROOT_PATH . 'includes/cls_template.php');
        $smarty = new cls_template;
        if (defined('IS_MOBILE')) {
            $smarty->template_dir   = ROOT_PATH . 'mobile/templates';
        } else {
            $smarty->template_dir   = ROOT_PATH . 'themes/default';
        }
        $smarty->caching        = false; // 是否禁用缓存
        $smarty->cache_lifetime = $_CFG['cache_time']; // 缓存时间
        $smarty->cache_dir      = ROOT_PATH . 'temp/caches'; // 缓存目录
        $smarty->compile_dir    = ROOT_PATH . 'temp/compiled'; // 解释目录

        $smarty->assign('lang', $_LANG); // 模板语言包赋值
        $smarty->assign('ecs_charset', 'utf-8'); // ECSHOP原设置模板编码（待删除）（新程序不得使用）
    } else {
        /* 加载最新模板引擎类 */
        require(ROOT_PATH . 'includes/smarty-3.1.24/Smarty.class.php');
        $smarty = new smarty();
        // 用cacheLifeTime来判断缓存文件是否过期。
        // $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
        // 禁用模板缓存
        $smarty->setCaching(false);
        // 设置模板目录
        if (defined('IS_MOBILE')) {
            $smarty->setTemplateDir(ROOT_PATH . 'mobile/templates');
            // 设置模板缓存编译目录
            $smarty->setCompileDir(ROOT_PATH . 'temp/compiled/mobile');
            // 设置模板缓存目录
            $smarty->setCacheDir(ROOT_PATH . 'temp/caches/mobile');
        } else {
            $smarty->setTemplateDir(ROOT_PATH . 'themes/default');
            // 设置模板缓存编译目录
            $smarty->setCompileDir(ROOT_PATH . 'temp/compiled');
            // 设置模板缓存目录
            $smarty->setCacheDir(ROOT_PATH . 'temp/caches');
        }
        // 设置缓存过期时间
        $smarty->setCacheLifetime($_CFG['cache_time']);
        // 设置调试模式
        $smarty->debugging = false;

        $smarty->assign('__ISNEW__', true);
    }

    $smarty->assign('__PUC__', __PUC__);
    $smarty->assign('__PUCM__', __PUCM__);
    $smarty->assign('__IMG__', __IMG__);
    $egg_img = 'http://test.eggms.com/'; // 图片目录地址
    $smarty->assign('egg_img', $egg_img);
    if (!empty($_CFG['stylename']))
    {
        $smarty->assign('ecs_css_path', 'themes/' . $_CFG['template'] . '/style_' . $_CFG['stylename'] . '.css');
    }
    else
    {
        /* my_style.css路径 */
        $smarty->assign('my_style_css', $egg_img . 'themes/' . $_CFG['template'] . '/public/my_style.css?1.1.1');

        $smarty->assign('ecs_css_my', $egg_img . 'themes/' . $_CFG['template'] . '/public/my_style.css?1.1.1');
        $smarty->assign('ecs_css_path', $egg_img . 'themes/' . $_CFG['template'] . '/public/style.css?1.1.1');
        $smarty->assign('ecs_css_category', $egg_img . 'themes/' . $_CFG['template'] . '/public/category.css?1.1.1');
        $smarty->assign('ecs_css_article', $egg_img . 'themes/' . $_CFG['template'] . '/public/article.css?1.1.1');
        $smarty->assign('ecs_css_index', $egg_img . 'themes/' . $_CFG['template'] . '/public/index.css?1.1.1');
        $smarty->assign('ecs_css_freeplan', $egg_img . 'themes/' . $_CFG['template'] . '/public/freeplan.css?1.1.1');
        $smarty->assign('ecs_css_genuine', $egg_img . 'themes/' . $_CFG['template'] . '/public/genuine.css?1.1.1');
        $smarty->assign('ecs_css_flash', $egg_img . 'themes/' . $_CFG['template'] . '/public/flash.css?1.1.1');
        $smarty->assign('ecs_css_glair', $egg_img . 'themes/' . $_CFG['template'] . '/public/glair.css?1.1.1');
        $smarty->assign('ecs_css_check_points', 'themes/' . $_CFG['template'] . '/public/check_points.css?1.1.1');
    }
    $smarty->assign('egg_img', $egg_img);
    $smarty->assign('url', 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}

/* 处理用户信息（待修正） */
if (!defined('INIT_NO_USERS'))
{
    /* 会员信息 */
    $user =& init_users();

    if (empty($_SESSION['user_id']))
    {
        if ($user->get_cookie())
        {
            /* 如果会员已经登录并且还没有获得会员的帐户余额、积分以及优惠券 */
            if ($_SESSION['user_id'] > 0)
            {
                update_user_info();
            }
        }
        else
        {
            $_SESSION['user_id']     = 0;
            $_SESSION['user_name']   = '';
            $_SESSION['email']       = '';
            $_SESSION['user_rank']   = 0;
            $_SESSION['discount']    = 1.00;
            if (!isset($_SESSION['login_fail']))
            {
                $_SESSION['login_fail'] = 0;
            }
        }
    }
}

/* 初始化内存类 */
require(ROOT_PATH . 'includes/classStock.php');
$virtualStock = new classStock();
